/**
 * Created by lixiaodi on 2019/04/11.
 */

mini.parse();
var tabs1 = mini.get('tabs1');					//tab
var gridTab1 = mini.get('datagrid1');			//待阅
var gridTab2 = mini.get('datagrid2');			//已阅

//初始化
$(function(){
	waitHttpList();
	//gridTab1.setData(data);
});
//当tab切换的时候
tabs1.on('beforeactivechanged',function (e){
	var idx = e.index;
	//待办
	if(idx == 0){
		console.log(idx);
		//gridTab1.setData(data);
		waitHttpList();
	//已办
	}else if(idx == 1){
		console.log(idx);
		alreadyHttpList();
	//历史
	}else{
		console.log(idx);
		historyHttpList();
	}
})
//绘制行内操作按钮(tab1)
function onActionRenderer(e) {
    var grid = e.sender;
    var record = e.record;
    var taskId = record.id;
    var gridId = grid.id;
    //var taskId = record._uid;
    var rowIndex = e.rowIndex;
    //根据数据表显示按钮
	var s;
	if(grid.id == 'datagrid1'){
		s = ' <a class="mini-button Edit_Button" href="javascript:handlingTask(\'' + taskId + ',' + gridId + '\')" >办理任务</a>'
		  	+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}else if(grid.id == 'datagrid2'){
		s = ' <a class="mini-button Edit_Button" href="javascript:flowProcess(\'' + taskId + ',' + gridId + '\')" >流程执行过程</a>'
 			+ ' <a class="mini-button" iconCls="icon-date" onclick="oldPostil(\'' + taskId + ',' + gridId + '\')">历史批注</a>'
 			+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}else{
		s = ' <a class="mini-button Edit_Button" href="javascript:flowProcess(\'' + taskId + ',' + gridId + '\')" >流程执行过程</a>'
			+ ' <a class="mini-button" iconCls="icon-date" onclick="oldPostil(\'' + taskId + ',' + gridId + '\')">历史批注</a>'
			+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}
    return s;
}
//弹出窗口路径前缀
var url = bootPATH + "../frame/frame1/pages/";
//办理任务
function handlingTask(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	var gridTab1Data = gridTab1.getData();
	var row;
	gridTab1Data.forEach((task,index)=>{
		if(task.id == taskarr[0]){
			row = gridTab1Data[index];
		}
	});
	console.log(row,'row++++++');
	//请求表单数据（带入到办理任务页面）
	var obj={
			cTableName: row.cTableName,// 主表的名称
			//cTableName: "baoxiaodan",// 主表的名称
            formId: row.formlistId,// 表单id
            //formId: 126,// 表单id
            mTableId: row.taskId  //列的id
            //mTableId: '1554861022123'  //列的id
	}
	$.ajax({
            url: ajaxUrl + "/formltems/formByMtableId.do",
            type: "POST",
            data: {
               obj: JSON.stringify(obj)
            },
            success: function (data) {
            	console.log(data, '获得编辑的数据');
            	//打开办理任务tab
	           	var taskHandle = {
	           		action: 'handle',
	           		taskId: row.taskId,
	           		id: row.id,
	           		taskName: row.taskName,
	           		formListId: row.formlistId,
	           		cTableName: row.cTableName,
	           		//cTableName: "baoxiaodan",
	           		edit_data: data[0]
	           	 };
	           	console.log(taskHandle,'需要传到子页面显示单据的');
                localStorage.removeItem("taskHandle");
                localStorage.setItem("taskHandle",JSON.stringify(taskHandle));
                //打开tab页面配置信息
	           	 var item = {
	           			 iconCls: "fa fa-desktop",
	           			 text: "审批",
	           			 url: "../pages/activiti/datagridDetail/approvalDetail.html"
	           	 };
	           	 //与父页面通信
	           	 parent.activeTab(item);
            },
            error: function (data) {
            	console.log(data);
            }
    });
}
//流程执行过程
function flowProcess(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	if(gridId == 'datagrid2'){
		var gridTab2Data = gridTab2.getData();
	}else if(gridId == 'datagrid3'){
		var gridTab3Data = gridTab3.getData();
	}
}
//历史批注
function oldPostil(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	if(gridId == 'datagrid2'){
		var gridTab2Data = gridTab2.getData();
	}else if(gridId == 'datagrid3'){
		var gridTab3Data = gridTab3.getData();
	}
}
//查看流程图
function seeImg(task){
	//得到被操作行数据
	var row = gridTab1.getRowByUID(row_uid);
    var taskId = row.taskId;
    $.ajax({
        url: ajaxUrl + "/activitiTaskController/showCurrentView.do?taskId=" + taskId ,
        type:"post",
        success: function (data) {
        	console.log(data);
            //grid.setData(data.data)
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
//待办刷新
function waitRefresh(){
	waitHttpList();
}
//已办刷新
function alreadyRefresh(){
	alreadyHttpList();
}
//待办list请求
function waitHttpList(){
	$.ajax({
        url: ajaxUrl + "/activitiTaskController/taskPage.do",
        type:"post",
        success: function (data) {
        	console.log(data);
        	var gridTab1Arr = data.data;
        	//时间格式
        	for(var i = 0; i < gridTab1Arr.length; i++){
        		//日期格式转换
    			if(gridTab1Arr[i].createTime){
    				gridTab1Arr[i].createTime = formatDate(gridTab1Arr[i].createTime);
    			}
        	}
        	gridTab1.setData(data.data);
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
//已办list请求
function alreadyHttpList(){
	$.ajax({
        url: ajaxUrl + "/activitiTaskHisController/taskUnfinishPage.do",
        type:"post",
        success: function (data) {
        	console.log(data);
        	var gridTab2Arr = data.data;
        	//时间格式
        	for(var i = 0; i < gridTab2Arr.length; i++){
        		//日期格式转换
    			if(gridTab2Arr[i].createTime){
    				gridTab2Arr[i].createTime = formatDate(gridTab2Arr[i].createTime);
    			}
        	}
        	gridTab2.setData(gridTab2Arr);
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
